/*
 * $newTable v1.0
 */

function submitForm(form,currentPage){
	  form.find('.page-current').val(currentPage);
    form.find('.page-size').val(form.find('.page-num').val());
    form.submit();
}
;(function($){
 	$newTable = {
 		getCurrentForm:function(el){
 			return $(el).parents('.table-form');
 		},
 		getCheckbox:function(el){
 			return $css.checkedVal('ids',$newTable.getCurrentForm(el));
 		},
 		getSelected:function(el,split){
 			var s=[];
 			$newTable.getCurrentForm(el).find('tr.listsel').each(function(i,t){
 	           s.push($(t).attr('ids'));
 	        })
 	        return s.join(split);
 		},
 		event: function(){
			$('body').on('click','.pagination .page-prev',function(){
		        var $form=$(this).parents('.table-form'),
		            prevPage=parseInt($form.find('.page-current').val())-1;
		        if(prevPage==0)
		        		prevPage=1; 
		        submitForm($form,prevPage);   
		    })
		    $('body').on('click','.pagination .page-next',function(){
		        var $form=$(this).parents('.table-form'),
		            pageCount=parseInt($form.find('.page-count').val()),
		            nextPage=parseInt($form.find('.page-current').val())+1;
		        if(nextPage>pageCount)
		            nextPage=pageCount;
		        submitForm($form,nextPage);   
		    })
		    $('body').on('click','.pagination .page-first',function(){
		        $form=$(this).parents('.table-form');
		        submitForm($form,1);   
		    })
		    $('body').on('click','.pagination .page-last',function(){
		        $form=$(this).parents('.table-form');
		        submitForm($form,$form.find('.page-count').val());   
		    })
		    $('body').on('click','.pagination .page-go',function(){
		        var $form=$(this).parents('.table-form'),
		            pageNum=parseInt($form.find('.page-jump').val());
		            pageCount=parseInt($form.find('.page-count').val());
		        if(pageNum>pageCount)
		            pageNum=pageCount;  
		        if(pageNum==0)
		            pageNum=1;      
		        $form=$(this).parents('.table-form');
		        submitForm($form,pageNum);   
		    })
		    $('body').on('keydown','.pagination .page-jump',function(event){
		    	var ENTER = 13;
		    	if(event.keyCode==ENTER){
		    		$form=$(this).parents('.table-form');
		    		$form.find('.page-current').val($form.find('.page-jump').val());
		        $form.submit();
		    	}
		    })
		    $('body').on('keydown','.pagination .page-num',function(){
		    	var ENTER = 13;
		    	if(event.keyCode==ENTER){
		    		$form=$(this).parents('.table-form');
		    		$form.find('.page-size').val($form.find('.page-num').val());
		        $form.submit();
		    	}
		    })
		    $('body').on('click','th[order-field]',function(){
		        var $this=$(this),
		            $form=$this.parents('.table-form'),
		            orderFlag=$form.find('.order-flag').val();
		        $form.find('.order-flag').val(1-orderFlag);
		        $form.find('.order-string').val($this.attr('order-field'));
		        $form.submit();
		    })
		    $('body').on('click','.cleck-all',function(){
		        var $this=$(this),
		            $form=$this.parents('.table-form');
		            group=$this.attr('group');
		        $form.find('input[name="'+group+'"]').prop('checked',$this.prop("checked"));
		    })
		    $('body').on('click','.btnTab a',function(){
		    	var $this=$(this),
		    	$form=$this.parents('.table-form');
		    	$this.parent().find('input').val($this.attr('value'));
		    	$form.submit();
		    })
		    $('body').on('click','td .expand',function(){
				var $this=$(this),
				  $i=$this.find('i'),
				  $sub=$this.parents('tr').next('.sub');
				  cla=$i.attr('class');
				  if(cla=="icon_arrow_up"){
				$i.attr('class','icon_arrow_down');
				    $sub.hide();
				  }else{
				    if(!$sub[0]){
				      $sub=$('<tr class="sub"><td colspan="'+$this.parents('tr').find('td').length+'">详细内容</td></tr>');
				  $sub.insertAfter($(this).parents('tr'));
				}
				$i.attr('class','icon_arrow_up');
				    $sub.show();
				  }
		    })
 		}
  };
})(jQuery)